<?
class excel{
	var $author="";
	var $lastAuthor="";
	var $company="";
	var $version="11.8132";
	var $debug=true;
	var $data;
	var $worksheet;
	var $worksheet_size=65536;
	function excel(){
		$this->data=array();
	}
	function new_worksheet($worksheet=""){
		$worksheet=(strlen($worksheet)<1)?"Worksheet ".count($this->data):$worksheet;
		if(!$this->sheetExists($worksheet)){
			$this->data[$worksheet]=array();
		}
	}
	function sheetExists($worksheet=""){
		return(array_key_exists($worksheet,$this->data));
	}
	function add_row($worksheet=""){
		$worksheet=(strlen($worksheet)<1)?count($this->data)-1:$worksheet;
		if(count($this->data)<1||!$this->sheetExists($worksheet)){
			$this->new_worksheet($worksheet);
		}
		$row=func_get_args();
		$row=array_splice($row,1);
		array_push($this->data[$worksheet],$row);
	}
	function splice_assoc(&$array,$key="",$length=0){
		$ar=array();
		if(array_key_exists($key,$array)){
			$ind=0;
			foreach($array as $tkey=>$tvalue){
				if($key==$tkey){
					break;
				}
				$ind++;
			}
			if($length==0){
				$ar=array_splice($array,$ind);
			}else{
				$ar=array_splice($array,$ind,$length);
			}
		}else{
			$ar=$array;
		}
		return($ar);
	}
	function is_worksheet_full($worksheet=""){
		$result=false;
		if(count($this->data[$worksheet])>=$this->worksheet_size){
			$result=true;
		}
		return($result);
	}
	function fix_worksheet_sizes(){
		$result=true;
		$tmp=array();
		foreach($this->data as $key=>$value){
			if(count($value)>$this->worksheet_size){
				$loops=ceil(count($value)/$this->worksheet_size);
				$tmp[$key]=$value;
				for($c=1;$c<$loops;$c++){
					$overflow=array_splice($tmp[$key],$this->worksheet_size,$this->worksheet_size);
					$sub_worksheet=$key." ".$c;
					if(!$this->sheetExists($sub_worksheet)){
						$tmp[$sub_worksheet]=$overflow;
					}else{
						$result=false;
					}
				}
			}else{
				$tmp[$key]=$value;
			}
		}
		$this->data=$tmp;
		return($result);
	}
	function output($fileName=""){
		$fileName=(strlen($fileName)<1)?"excel_doc_".date("YMd_his",time()).".xml":$fileName;
		
		if(!$this->fix_worksheet_sizes()){
			print("Error fixing worksheets");
		}else{
			if(!$this->debug){
				header("content-type:application/vnd.ms-excel\n\n");
				header("content-disposition:attachment;filename=".$fileName.";\n\n");
				header("Pragma: no-cache;\n\n");
				header("Expires: 0;\n\n");
			}
			
			print("<?xml version=\"1.0\"?>".chr(13));
			print("<?mso-application progid=\"Excel.Sheet\"?>".chr(13));
			print("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">".chr(13));
			print("	<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">".chr(13));
			print("		<Author>".$this->author."</Author>".chr(13));
			print("		<LastAuthor>".$this->author."</LastAuthor>".chr(13));
			print("		<Created>".date("Y-M-d\TH:i:s\Z",time())."</Created>".chr(13));
			print("		<Company>".$this->company."</Company>".chr(13));
			print("		<Version>".$this->version."</Version>".chr(13));
			print("	</DocumentProperties>".chr(13));
			print("	<Styles>".chr(13));
			print("		<Style ss:ID=\"Default\" ss:Name=\"Normal\">".chr(13));
			print("			<Alignment ss:Vertical=\"Bottom\"/>".chr(13));
			print("			<Borders/>".chr(13));
			print("			<Font/>".chr(13));
			print("			<Interior/>".chr(13));
			print("			<NumberFormat/>".chr(13));
			print("			<Protection/>".chr(13));
			print("		</Style>".chr(13));
			print("	</Styles>".chr(13));
			
			foreach($this->data as $key=>$value){
				print("	<Worksheet ss:Name=\"".$key."\">".chr(13));
				print("		<Table>".chr(13));
				for($y=0;$y<count($value);$y++){
					print("			<Row>".chr(13));
					for($x=0;$x<count($value[$y]);$x++){
						print("			<Cell>".chr(13));
						print("				<Data ss:Type=\"String\">".$value[$y][$x]."</Data>".chr(13));
						print("			</Cell>".chr(13));
					}
					print("			</Row>".chr(13));
				}
				print("		</Table>".chr(13));
				print("	</Worksheet>".chr(13));
			}
			
			print("</Workbook>".chr(13));
			#print_r($value);
		}
	}
}
?>